package com.linkface.bankcard;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.linkface.base.LFGlobalVar;
import com.linkface.card.CardActivity;
import com.linkface.card.CardScanner;
import com.linkface.utils.LFWindowToast;

import cn.linkface.ocr.LFCard;
import cn.linkface.ocr.LFCardRecognizeListener;

/**
 * Copyright (c) 2017-2018 LINKFACE Corporation. All rights reserved.
 */
public class BankCardActivity extends CardActivity {
    @Override
    protected CardScanner initCardScanner(Context context, int currentFrameOrientation, boolean isVertical) {
        return new BankCardScanner(context, currentFrameOrientation, isVertical);
    }

    @Override
    protected void onClickRotateCardBtn(View view) {
        setCardOrientationVertical(!getCardOrientationVertical());
        clearOverlayView();
        initOverlayView();
        refreshRightTitleBtnView();
        CardScanner cardScanner = getCardScanner();
        if (cardScanner != null) {
            cardScanner.resetStartDetectTime();
        }
    }

    /**
     * 竖向时 竖卡&拍照识别时 需将扫描框调的稍微小一点 否则布局中元素会重叠
     */
    @Override
    protected void adjustCardScanRect(Rect cardScanRect, int svWidth, int svHeight) {
        if (mCardOrientationVertical && mScanIsManualRecognize) {
            // 在cardScardRect原有比例的基础上 继续以8/5的比例缩小
            float scale = 1.12f;
            int newWidth = (int) (cardScanRect.width() / scale);        // 此处不应使用屏幕实际宽度，而是应该用SurfaceView实际画面显示的宽高 因为屏幕可能存在黑边 会导致计算的扫描框存在偏移
            int newHeight = (int) (cardScanRect.height() / scale);
            int left = (svWidth - newWidth) / 2;
            int top = (svHeight - newHeight) / 2;     // 高不变
            int right = left + newWidth;
            int bottom = top + newHeight;
            cardScanRect.set(left, top, right, bottom);
        }
    }

    @Override
    protected void refreshRightTitleBtnView() {
        // 银行卡 横竖卡切换按钮可见
        mIvTitleRight.setVisibility(View.VISIBLE);
        int resId = getRightButtonResId();
        mIvTitleRight.setImageResource(resId);

        if (!mScanIsManualRecognize) {
            // 银行卡自动识别时，切换横竖卡按钮位于右上角 "相册"不可见
            RelativeLayout.LayoutParams resultParams = (RelativeLayout.LayoutParams) mIvTitleRight.getLayoutParams();
            resultParams.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            resultParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            mIvTitleRight.setLayoutParams(resultParams);
        } else {
            // 相册可见，而且位于右上角
            mTvTitleRight.setVisibility(View.VISIBLE);
            // 横竖卡切换图标会放到右下角（布局中默认就是放在右下角）
        }
    }

    @Override
    protected void processManualRecognize() {
        final Bitmap confirmBmp = LFGlobalVar.sCardBitmap;

        if (confirmBmp == null || confirmBmp.isRecycled()) {
            Toast.makeText(BankCardActivity.this, "手动确认图片不能为空！", Toast.LENGTH_SHORT).show();
            return;
        }

        final LFWindowToast globalToast = new LFWindowToast();
        globalToast.makeToast(BankCardActivity.this, "识别中", 3000).show();

        try {
            getCardScanner().getCardRecognizer().recognizeCard(0, confirmBmp, getCardOrientationVertical(), null, new LFCardRecognizeListener() {
                @Override
                public void onRecognizeSuccess(final Bitmap bitmap,final LFCard card) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            globalToast.hide();
                            goToResultActivity(card, bitmap);
                        }
                    });
                }

                @Override
                public void onRecognizeFail() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            globalToast.hide();
                            LFWindowToast wToast = new LFWindowToast();
                            wToast.makeToast(BankCardActivity.this, "识别失败！", 500).show();
                        }
                    });
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
